export async function useCanvasPoster(canvasId, dateobj) {

	let imgUrl = dateobj.bgimg; //网络资源背景图片
	let posterQrCodeImg = dateobj.codeimg; //二维码
	let logo = dateobj.logo
	let logoname = dateobj.logoname
	let userName = dateobj.titlename;
	let money = dateobj.money
	let btneme = dateobj.btneme

	const ctx = uni.createCanvasContext(canvasId, this);

	const drawPoster = async () => {
		ctx.fillStyle = '#ffffff';
		ctx.fillRect(0, 0, 750, 1200); // 绘制背景
		if (userName && userName.name) {
			ctx.fillStyle = userName.color
			ctx.setFontSize(userName.size)
			ctx.fillText(userName.name, userName.l, userName.t); // 绘制文本
			ctx.draw(true)
		}

		if (logoname && logoname.name) {
			ctx.setFontSize(logoname.size);
			//字体颜色
			ctx.setFillStyle(logoname.color);
			ctx.fillText(logoname.name, logoname.l, logoname.t);
			ctx.draw(true)
		}

		if (money && money.name) {
			ctx.fillStyle = money.color
			ctx.setFontSize(money.size)
			ctx.fillText(money.name, money.l, money.t); // 绘制文本
			//字体颜色
			ctx.setFontSize(34);
			ctx.setFillStyle('#B63434');
			ctx.fillText("￥ ", 20, money.t);
			ctx.draw(true)
		}
		if (btneme) {
			ctx.setFontSize(btneme.size);
			//字体颜色
			ctx.setFillStyle(btneme.color);
			let btnemes = btneme.name.length > 40 ? btneme.name.substring(0, 42) + "..." : btneme.name;
			ctx.lineWidth = 1;
			var str = btnemes
			var lineWidth = 0;
			var canvasWidth = 710; //计算canvas的宽度
			var initHeight = 15; //绘制字体距离canvas顶部初始的高度
			var lastSubStrIndex = 0; //每次开始截取的字符串的索引
			console.log(str)

			for (let i = 0; i < str.length; i++) {
				lineWidth += ctx.measureText(str[i]).width;
				if (lineWidth > canvasWidth) {
					console.log("asdas", str[i])
					ctx.fillText(str.substring(lastSubStrIndex, i), btneme.l, btneme.t); //绘制截取部分
					initHeight += 40; //20为字体的高度
					lineWidth = 0;
					lastSubStrIndex = i;
				}
				if (i == str.length - 1) { //绘制剩余部分  debugger

					ctx.fillText(str.substring(lastSubStrIndex, i + 1), btneme.l, btneme.t += btneme
						.size + 10);


				}
			}
			ctx.draw(true)
			// ctx.fillText(btneme, props.btneme.l, props.btneme.t,710);
		}


		let temp = await drawImagefun(ctx, canvasId, imgUrl);
		let temp2 = await drawImagefun(ctx, canvasId, posterQrCodeImg);
		let temp3 = await drawImagefun(ctx, canvasId, logo);


		// console.log("111111111111")
		// let canvasimage = await canvasToTempFilePath(ctx, canvasId)
		// return true
		// let canvasimage = await canvasToTempFilePath(ctx, canvasId)
		return temp3


		// 这里可以添加更多的绘制逻辑，比如绘制图片、绘制边框等




	};

	let temp2 = await drawPoster();
	console.log(temp2, "temp2")



	return temp2
}


function drawImagefun(ctx, canvasId, obj) {
	if (obj) {
		return new Promise((resolve, reject) => {
			uni.getImageInfo({
				src: obj.url,
				success: async (res) => {
					console.log(res)
					console.log("111111111111", res)
					ctx.drawImage(res.path, obj && obj.l, obj && obj.t, obj && obj.w, obj && obj.h);
					ctx.draw(true, setTimeout(() => {
						resolve(res)
					}, 300))

					console.log("sadas3")
				}
			})
		});
	}

}

// 将 canvasToTempFilePath 封装成一个返回 Promise 的函数
function canvasToTempFilePath(ctx, canvas) {
	console.log("sadas1", ctx, canvas)
	return new Promise(async (resolve, reject) => {
		await uni.canvasToTempFilePath({
			canvasId: "posterCanvas",
			quality: 1,
			success: function(res) {

				resolve(res.tempFilePath)
				// uni.hideLoading();
				// emit('childEvent', urlindex.value);

			},
			fail: function(e) {
				// emit('childEvent', "");
				reject(e)
			}
		});
		console.log("sadas2")
		// await ctx.(true, setTimeout(() => {
		// 		console.log(3)

		// 	}, 300)




		// );

	});
}